﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;

namespace Web.admin.InnerManage
{
    public partial class AddStuSummry : System.Web.UI.Page
    {
        ParticipantService participantService = new ParticipantService();
        ContestService contestService = new ContestService();
       
        protected void Page_Load(object sender, EventArgs e)
        {
             if (!IsPostBack)
            {
                string operation = Request["OPERATION"];
                string conId = Request["ContestId"];
                string parid = Request["parid"];

                if (operation == null)
                {
                    BoContests contest = contestService.getContest(conId);
                    if (contest == null)
                    {
                        return;
                    }
                    ContestName.Text = contest.CON_NAME;
                    ConId.Value = conId;
                    FileTR.Visible = true;
                    return;
                }                

                if (operation == "Modify")
                {
                    ModifySummary(parid);
                }
                else if (operation == "Delete")
                {
                    DeleteSummary(parid);
                }
            }
        }
         protected void ModifySummary(string particiId)
        {
             ParId.Value = particiId;
             BoParticipants participant=new BoParticipants();
             participant=participantService.getParticipant(particiId);

             stuCode_txt.Value = participant.STU_CODE;
             sumtitle.Text = participant.PAR_SUMTITLE;
             KindEditor.Text = participant.PAR_SUMMARY;
             txtDate.Text =participant.PAR_SUMTIME.ToString();
             Image1.ImageUrl = "/UploadImages/" + participant.PAR_SUMURL;
             FileTR.Visible = true;

             BoContests contest = contestService.getContest(participant.PAR_CONTESTS);
             if (contest == null)
             {
                 return;
             }
             ContestName.Text = contest.CON_NAME;
             ConId.Value = participant.PAR_CONTESTS;
        }

        protected void DeleteSummary(string particiId)
        {
            BoParticipants par=participantService.getParticipant(particiId);
            DeleteImageFile(par);
            par.PAR_SUMURL = "";
            par.PAR_SUMTITLE = "";
            par.PAR_SUMMARY = "";
            par.PAR_SUMTIME = DateTime.MinValue;
            participantService.UpdataSummary(par);
            Response.Redirect("StuSummary.aspx?id=" + par.PAR_CONTESTS);
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            BoParticipants par=null;

           
            if (string.IsNullOrEmpty(ParId.Value))
            {
                //par=new BoParticipants();
                par = participantService.GetParticipantsByCode(stuCode_txt.Value, ConId.Value);
            }
            else
                par=participantService.getParticipant(ParId.Value);

            par.PAR_SUMMARY=KindEditor.Text;
            par.PAR_SUMTITLE=sumtitle.Text;
            par.PAR_SUMTIME=DateTime.Parse(txtDate.Text);
            par.PAR_SUMURL= Image1.ImageUrl;

            participantService.UpdataSummary(par);

            Response.Redirect("StuSummary.aspx?id=" + par.PAR_CONTESTS);
        }

        public String SaveImageFile()
        {
            Boolean fileOK = false;        
            String path = Server.MapPath("~/UploadImages/");        
            if (FileUpload1.HasFile)         
            {            
                String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();            
                String[] allowedExtensions = {".txt", ".doc", ".docx", ".pdf"};          
                for (int i = 0; i < allowedExtensions.Length; i++)          
                {               
                    if (fileExtension == allowedExtensions[i])               
                    {                    
                        fileOK = true;               
                    }          
                }        
            }         
            if (fileOK)        
            {            
                try            
                {
                    String pre = Guid.NewGuid().ToString();
                    FileUpload1.PostedFile.SaveAs(path + pre + "_" + FileUpload1.FileName);
                    return pre + "_" + FileUpload1.FileName;
                }            
                catch (Exception ex)            
                {
                    throw new Exception("文件保存失败！：" + ex.Message);
                }        
            }        
            else        
            {
                throw new Exception("不支持的文件格式");
            }    
        }

        public void DeleteImageFile(BoParticipants par)
        {
            String path = Server.MapPath("~/UploadImages/");
            String imagePath = path + par.PAR_SUMURL;
            System.IO.File.Delete(imagePath);
        }
    
    }
}